<template>
	<div class="maintain">
		<x-header>编辑设施信息
      <a href="javascript:;" class="save" @click="saveAction(submitType)">保存</a>
		  <a href="javascript:;" class="submit" @click="submitAction">提交</a>
    </x-header>
    <!-- {{CONS_NO}} -->
		<div class="maintainContent contractContent">
      <div class="information-item">
        <div class="information-item-title">归属分类</div>
        <popup-picker class="information-item-input" :data="FACILITY_CODE_LIST" show-name v-model="FACILITY_CODE"  @on-change="onPopupChange($event, 'FACILITY_CODE')" placeholder="请选择"></popup-picker>
        <!-- <img  class="information-item-rightImg" src="../../assets/img/securityCheckImg/rightArrow.png" alt=""> -->
      </div>
      <div class="information-item">
        <div class="information-item-title">上级设施</div>
         <popup-picker class="information-item-input" v-if="P_FACILITY_ID_LIST.length > 0" show-name :data="P_FACILITY_ID_LIST" v-model="P_FACILITY_ID" @on-change="onPopupChange($event, 'P_FACILITY_ID')" placeholder="请选择"></popup-picker>
        <input class="information-item-input" disabled v-else placeholder="暂无上级设施" value="">
        <!-- <img  class="information-item-rightImg" src="../../assets/img/securityCheckImg/rightArrow.png" alt=""> -->
      </div>
      <div class="information-item">
        <div class="information-item-title">设施编号</div>
        <input class="information-item-input"  v-model="FACILITY_NO" type="text" maxlength="16" placeholder="请录入" value="">
      </div>
      <div class="information-item">
        <div class="information-item-title">设施名称</div>
        <input class="information-item-input" v-model="FACILITY_NAME" type="text" maxlength="16" placeholder="请录入" value="">
      </div>
      <div class="information-item">
        <div class="information-item-title">地理位置</div>
        <input class="information-item-input" v-model="ADDR_POSITION" type="text" maxlength="16" placeholder="请录入" value="">
      </div>
      <div class="information-item">
        <div class="information-item-title">规格型号</div>
         <popup-picker class="information-item-input" :data="FACILITY_MODEL_LIST" show-name v-model="FACILITY_MODEL"  @on-change="onPopupChange($event,'FACILITY_MODEL')"  placeholder="请选择"></popup-picker>
        <!-- <img  class="information-item-rightImg" src="../../assets/img/securityCheckImg/rightArrow.png" alt=""> -->
      </div>
      <div class="information-item">
        <div class="information-item-title">材料形式</div>
         <popup-picker class="information-item-input" :data="FACILITY_QUALITY_LIST" show-name v-model="FACILITY_QUALITY" @on-change="onPopupChange($event,'FACILITY_QUALITY')"  placeholder="请选择"></popup-picker>
        <!-- <img  class="information-item-rightImg" src="../../assets/img/securityCheckImg/rightArrow.png" alt=""> -->
      </div>
      <div class="information-item">
        <div class="information-item-title">连接方式</div>
         <popup-picker class="information-item-input" :data="CONNECTION_MODE_LIST" show-name v-model="CONNECTION_MODE" @on-change="onPopupChange($event,'CONNECTION_MODE')"  placeholder="请选择"></popup-picker>
        <!-- <img  class="information-item-rightImg" src="../../assets/img/securityCheckImg/rightArrow.png" alt=""> -->
      </div>
      <div class="information-item">
        <div class="information-item-title">安装方式</div>
         <popup-picker class="information-item-input" :data="INSTALL_MODE_LIST" show-name v-model="INSTALL_MODE"  @on-change="onPopupChange($event,'INSTALL_MODE')"  placeholder="请选择"></popup-picker>
        <!-- <img  class="information-item-rightImg" src="../../assets/img/securityCheckImg/rightArrow.png" alt=""> -->
      </div>
     <!--  <div class="information-item">
        <div class="information-item-title">安装位置</div>
         <popup-picker class="information-item-input" :data="list1" v-model="value1" @on-show="onShow" @on-hide="onHide" @on-change="onChange" placeholder="请选择"></popup-picker>
        <img  class="information-item-rightImg" src="../../assets/img/securityCheckImg/rightArrow.png" alt="">
      </div> -->
      <!-- <div class="information-item">
        <div class="information-item-title">当前位置</div>
         <popup-picker class="information-item-input" :data="CURRENT_STATE_LIST" show-name v-model="CURRENT_STATE" @on-show="onShow" @on-hide="onHide" @on-change="onChange" placeholder="请选择"></popup-picker>
        <img  class="information-item-rightImg" src="../../assets/img/securityCheckImg/rightArrow.png" alt="">
      </div> -->
      <div class="information-item">
        <div class="information-item-title">当前状态</div>
         <popup-picker class="information-item-input" :data="CURRENT_STATE_LIST" show-name v-model="CURRENT_STATE" @on-change="onPopupChange($event,'CURRENT_STATE')" placeholder="请选择"></popup-picker>
        <!-- <img  class="information-item-rightImg" src="../../assets/img/securityCheckImg/rightArrow.png" alt=""> -->
      </div>
    </div>
    <div class="information-addIcon"  @click="addIconClick">
      <div class="information-addIcon-div">
        <img class="information-addIcon-icon" src="../../assets/img/securityCheckImg/addHeader.png" alt="">
      </div>
    </div>
	</div>
</template>

<script>
import { XHeader, PopupPicker } from "vux";
import Vue from "vue";
import { getsn } from "@/utils/getsn";
import {
  userInfoFn,
  sqlQuery,
  scanNewMeterAction,
  sqlInsert,
  sqlUpdata,
  submitremoval
} from "@/utils/sqlOperation";
export default {
  data() {
    return {
      value1: ["居民"],
      title1: "手机机型",
      list1: [
        ["小米1", "iPhone1", "华为1", "情怀1", "三星1", "其他1", "不告诉你1"]
      ],

      CERT_TYPE: "",
      FACILITY_CODE: [],
      FACILITY_CODE_LIST: [],
      P_FACILITY_ID: [],
      P_FACILITY_ID_LIST: [],
      FACILITY_NO: [],
      FACILITY_NAME: [],
      ADDR_POSITION: [],
      FACILITY_MODEL_LIST: [],
      FACILITY_MODEL: [],
      FACILITY_QUALITY_LIST: [],
      FACILITY_QUALITY: [],
      CONNECTION_MODE_LIST: [],
      CONNECTION_MODE: [],
      INSTALL_MODE_LIST: [],
      INSTALL_MODE: [],
      CURRENT_STATE_LIST: [],
      CURRENT_STATE: [],
      CONS_NO: "",
      change_type: 1,
      FACILITY_ID: "",
      M_CID: "",
      submitType: 1,
      P_M_CID_LIST: [],
      ORG_NO: '',
    };
  },
  components: {
    XHeader,
    PopupPicker
  },
  mounted() {
    Vue.set(this, "CONS_NO", localStorage.getItem("CONS_NO"));
    const _that = this;
    // 归属分类1
    AndroidApi.queryTableData(
      '{"whereClause":"select * from P_CODE where code_type= ?","whereArgs":["mdmFacilityNature"]}',
      function(msg) {
        if (msg.resultCode === "9999") {
          const FACILITY_CODE_LIST = msg.result.map(item => {
            return {
              name: item.CODE_NAME,
              value: item.CODE_VALUE
            };
          });
          Vue.set(_that, "FACILITY_CODE_LIST", [FACILITY_CODE_LIST]);
        }
      }
    );
    // 规格型号3
    AndroidApi.queryTableData(
      '{"whereClause":"select * from P_CODE where code_type= ?","whereArgs":["mdmFacilityModel"]}',
      function(msg) {
        if (msg.resultCode === "9999") {
          const FACILITY_MODEL_LIST = msg.result.map(item => {
            return {
              name: item.CODE_NAME,
              value: item.CODE_VALUE
            };
          });
          Vue.set(_that, "FACILITY_MODEL_LIST", [FACILITY_MODEL_LIST]);
        }
      }
    );
    // 材料形式4
    AndroidApi.queryTableData(
      '{"whereClause":"select * from P_CODE where code_type= ?","whereArgs":["bpPartProperty"]}',
      function(msg) {
        if (msg.resultCode === "9999") {
          const FACILITY_QUALITY_LIST = msg.result.map(item => {
            return {
              name: item.CODE_NAME,
              value: item.CODE_VALUE
            };
          });
          Vue.set(_that, "FACILITY_QUALITY_LIST", [FACILITY_QUALITY_LIST]);
        }
      }
    );
    // 连接方式5
    AndroidApi.queryTableData(
      '{"whereClause":"select * from P_CODE where code_type= ?","whereArgs":["mdmFacilityConmode"]}',
      function(msg) {
        if (msg.resultCode === "9999") {
          const CONNECTION_MODE_LIST = msg.result.map(item => {
            return {
              name: item.CODE_NAME,
              value: item.CODE_VALUE
            };
          });
          Vue.set(_that, "CONNECTION_MODE_LIST", [CONNECTION_MODE_LIST]);
        }
      }
    );
    // 安装方式6
    AndroidApi.queryTableData(
      '{"whereClause":"select * from P_CODE where code_type= ?","whereArgs":["mdmFacilityInsmode"]}',
      function(msg) {
        if (msg.resultCode === "9999") {
          const INSTALL_MODE_LIST = msg.result.map(item => {
            return {
              name: item.CODE_NAME,
              value: item.CODE_VALUE
            };
          });
          Vue.set(_that, "INSTALL_MODE_LIST", [INSTALL_MODE_LIST]);
        }
      }
    );
    // 安装位置7
    // AndroidApi.queryTableData(
    //   '{"whereClause":"select * from P_CODE where code_type= ?","whereArgs":["mdmEquiplnsddr"]}',
    //   function(msg) {
    //     if (msg.resultCode === "9999") {
    //       const CERT_TYPE_LIST = msg.result.map(item => {
    //         return {
    //           name: item.CODE_NAME,
    //           value: item.CODE_VALUE
    //         };
    //       });
    //       Vue.set(_that, "CERT_TYPE_LIST", [CERT_TYPE_LIST]);
    //     }
    //   }
    // );
    // 当前位置8
    // AndroidApi.queryTableData(
    //   '{"whereClause":"select * from P_CODE where code_type= ?","whereArgs":["cdReCertType"]}',
    //   function(msg) {
    //     if (msg.resultCode === "9999") {
    //       const CURRENT_STATE_LIST = msg.result.map(item => {
    //         return {
    //           name: item.CODE_NAME,
    //           value: item.CODE_VALUE
    //         };
    //       });
    //       Vue.set(_that, "CURRENT_STATE_LIST", [CURRENT_STATE_LIST]);
    //     }
    //   }
    // );
    // 目前状态9
    AndroidApi.queryTableData(
      '{"whereClause":"select * from P_CODE where code_type= ?","whereArgs":["mdmFacilityState"]}',
      function(msg) {
        if (msg.resultCode === "9999") {
          const CURRENT_STATE_LIST = msg.result.map(item => {
            return {
              name: item.CODE_NAME,
              value: item.CODE_VALUE
            };
          });
          Vue.set(_that, "CURRENT_STATE_LIST", [CURRENT_STATE_LIST]);
        }
      }
    );

    // 赋值
    const _this = this;
    if (_this.$route.query.item) {
      const item = JSON.parse(_this.$route.query.item);
      _this.M_CID = item.M_CID;
      _this.FACILITY_ID = item.FACILITY_ID;
      _this.CONS_NO = item.CONS_NO;
      _this.FACILITY_CODE[0] = item.FACILITY_CODE;
      _this.P_FACILITY_ID[0] = (item.P_FACILITY_ID=='0'?'':item.P_FACILITY_ID);
      _this.FACILITY_NO = item.FACILITY_NO;
      _this.FACILITY_NAME = item.FACILITY_NAME;
      _this.ADDR_POSITION = item.ADDR_POSITION;
      _this.FACILITY_MODEL[0] = item.FACILITY_MODEL;
      _this.FACILITY_QUALITY[0] = item.FACILITY_QUALITY;
      _this.CONNECTION_MODE[0] = item.CONNECTION_MODE;
      _this.INSTALL_MODE[0] = item.INSTALL_MODE;
      _this.CURRENT_STATE[0] = item.CURRENT_STATE;
      Vue.set(_this, "change_type", _this.$route.query.change_type);
    }
    console.log(111111, this.P_FACILITY_ID[0]);
    userInfoFn(this.CONS_NO, this.userInfoCallback)
    if (this.P_FACILITY_ID[0] == "") {
      this.P_FACILITY_ID = [];
    }
    if (this.change_type == 2) {
      this.sqlQuery(
        this.CONS_NO,
        this.M_CID,
        "PIPE_VALVEINFO",
        this.facilityCallback
      );
    } else {
      sqlQuery(this.CONS_NO, "PIPE_VALVEINFO", this.facilityCallback);
    }
  },
  methods: {
    userInfoCallback(msg){
      console.log('用户信息查询',msg);
      Vue.set(this, "ORG_NO", msg[0].ORG_NO);
    },
    sqlQuery(cons_no, m_cid, tableName, callback) {
      const _this = this;
      const changeName = (tableName + "_CHANGE").toUpperCase();
      console.log(changeName);
      console.log(tableName);
      console.log(cons_no);
      AndroidApi.queryTableData(
        JSON.stringify({
          whereClause:
            "SELECT * FROM " +
            changeName +
            " A  WHERE A.CONS_NO=? AND A.M_CID <> ? AND A.P_M_CID <> ?;",
          whereArgs: [cons_no, m_cid, m_cid]
        }),
        function(msg) {
          if (msg.result.length > 0) {
            callback("change", msg.result);
          } else {
            Vue.set(_this, "P_M_CID_LIST", msg.result);
          }
        }
      );
      AndroidApi.queryTableData(
        JSON.stringify({
          whereClause:
            "SELECT *  FROM " +
            tableName.toUpperCase() +
            " B WHERE B.CONS_NO=? AND B.M_CID NOT IN (SELECT A.M_CID FROM " +
            changeName +
            " A  WHERE A.CONS_NO=? AND A.M_CID <> ? AND A.P_M_CID <> ?);",
          whereArgs: [cons_no, cons_no, m_cid, m_cid]
        }),
        function(msg) {
          console.log(3, msg);
          if (msg.result.length > 0) {
            callback("noChange", msg.result);
          } else {
            Vue.set(
                _this,
                "P_M_CID_LIST",
                _this.P_M_CID_LIST.concat(msg.result)
              );
          }
        }
      );
      console.log(_this.P_M_CID_LIST);
    },
    facilityCallback(str, msg) {
      switch (str) {
        case "change":
          const P_FACILITY_ID_LIST = msg.map(item => {
            return {
              name: item.FACILITY_NAME + "_" + item.ADDR_POSITION,
              value: item.M_CID
            };
          });
          Vue.set(this, "P_FACILITY_ID_LIST", [P_FACILITY_ID_LIST]);
          Vue.set(this, "P_M_CID_LIST", msg);
          break;
        case "noChange":
          const P_FACILITY_ID_LIST2 = msg.map(item => {
            return {
              name: item.FACILITY_NAME + "_" + item.ADDR_POSITION,
              value: item.M_CID
            };
          });
          Vue.set(this, "P_FACILITY_ID_LIST", [
            this.P_FACILITY_ID_LIST.concat(P_FACILITY_ID_LIST2)
          ]);
          Vue.set(this, "P_M_CID_LIST", this.P_M_CID_LIST.concat(msg));
          break;
        default:
          break;
      }
    },
    callback(str, msg) {
      if (this.submitType == 1) {
        if (msg.resultCode === "9999") {
          if (str == "sqlInsert") {
            this.$vux.toast.text("添加成功");
             Vue.set(this, "change_type", 2);
          } else if (str == "sqlUpdata") {
            this.$vux.toast.text("修改成功");
          }
        } else {
          this.$vux.toast.text("失败");
        }
      } else {
        this.$vux.toast.text("提交成功");
      }
    },
    saveAction(type) {
      Vue.set(this, "submitType", type);
      const P_FACILITY_ID =
        this.P_FACILITY_ID.length == 0
          ? "0"
          : this.rturnFACILITYIDFN(this.P_FACILITY_ID[0]);
      if (this.change_type == 2) {
        let data = [
          {
            CHANGE_TYPE: type == 2 ? "3" : "2",
            FACILITY_CODE: this.FACILITY_CODE[0],
            P_FACILITY_ID:
              P_FACILITY_ID == "0" ? "0" : P_FACILITY_ID.FACILITY_ID,
            FACILITY_NO: this.FACILITY_NO,
            FACILITY_NAME: this.FACILITY_NAME,
            ADDR_POSITION: this.ADDR_POSITION,
            FACILITY_MODEL: this.FACILITY_MODEL[0],
            FACILITY_QUALITY: this.FACILITY_QUALITY[0],
            CONNECTION_MODE: this.CONNECTION_MODE[0],
            INSTALL_MODE: this.INSTALL_MODE[0],
            CURRENT_STATE: this.CURRENT_STATE[0],
            P_M_CID: P_FACILITY_ID == "0" ? "0" : P_FACILITY_ID.M_CID
          }
        ];
        //修改
        sqlUpdata(
          "PIPE_VALVEINFO_CHANGE",
          JSON.parse(this.$route.query.item).M_CID,
          this.CONS_NO,
          data,
          this.callback
        );
      } else {
        const M_CID = getsn();
        Vue.set(this, "M_CID", M_CID);
        let data = [
          {
            M_CID: M_CID,
            APP_NO: "",
            CHANGE_TYPE: type == 2 ? "3" : "1",
            CONS_NO: this.CONS_NO,
            FACILITY_CODE: this.FACILITY_CODE[0],
            P_FACILITY_ID:
              P_FACILITY_ID == "0" ? "0" : P_FACILITY_ID.FACILITY_ID,
            FACILITY_NO: this.FACILITY_NO,
            FACILITY_NAME: this.FACILITY_NAME,
            ADDR_POSITION: this.ADDR_POSITION,
            FACILITY_MODEL: this.FACILITY_MODEL[0],
            FACILITY_QUALITY: this.FACILITY_QUALITY[0],
            CONNECTION_MODE: this.CONNECTION_MODE[0],
            INSTALL_MODE: this.INSTALL_MODE[0],
            CURRENT_STATE: this.CURRENT_STATE[0],
            P_M_CID: P_FACILITY_ID == "0" ? "0" : P_FACILITY_ID.M_CID
          }
        ];
        console.log(data);
        sqlInsert("PIPE_VALVEINFO_CHANGE", data, this.callback);
      }
    },
    addIconClick() {
      this.FACILITY_CODE = [];
      this.P_FACILITY_ID = [];
      this.FACILITY_NO = "";
      this.FACILITY_NAME = "";
      this.ADDR_POSITION = "";
      this.FACILITY_MODEL = [];
      this.FACILITY_QUALITY = [];
      this.CONNECTION_MODE = [];
      this.INSTALL_MODE = [];
      this.CURRENT_STATE = [];
      Vue.set(this, "change_type", 1);
    },
    onPopupChange(val, str) {
      console.log("val change", val);
      switch (str) {
        case "FACILITY_CODE":
          this.FACILITY_CODE = val;
          break;
        case "P_FACILITY_ID":
          this.P_FACILITY_ID = val;
          break;
        case "FACILITY_MODEL":
          this.FACILITY_MODEL = val;
          break;
        case "FACILITY_QUALITY":
          this.FACILITY_QUALITY = val;
          break;
        case "CONNECTION_MODE":
          this.CONNECTION_MODE = val;
          break;
        case "INSTALL_MODE":
          this.INSTALL_MODE = val;
          break;
        case "CURRENT_STATE":
          this.CURRENT_STATE = val;
          break;
        default:
          break;
      }
    },
    rturnFACILITYIDFN(val) {
      for (let i in this.P_M_CID_LIST) {
        if (this.P_M_CID_LIST[i].M_CID == val) {
          return this.P_M_CID_LIST[i];
        }
      }
    },
    submitAction: function() {
      console.log("提交");
      const P_FACILITY_ID =
        this.P_FACILITY_ID.length == 0
          ? "0"
          : this.rturnFACILITYIDFN(this.P_FACILITY_ID[0]);
      const data = {
        FACILITY_INFO: [
          {
            FACILITY_ID: "",
            FACILITY_CODE:
              this.FACILITY_CODE.length == 0 ? "" : this.FACILITY_CODE[0],
            FACILITY_TYPE: "",
            FACILITY_NAME: this.FACILITY_NAME,
            FACILITY_MODEL:
              this.FACILITY_MODEL.length == 0 ? "" : this.FACILITY_MODEL[0],
            FACILITY_QUALITY:
              this.FACILITY_QUALITY.length == 0 ? "" : this.FACILITY_QUALITY[0],
            CONNECTION_MODE:
              this.CONNECTION_MODE.length == 0 ? "" : this.CONNECTION_MODE[0],
            INSTALL_MODE:
              this.INSTALL_MODE.length == 0 ? "" : this.INSTALL_MODE[0],
            CURRENT_STATE:
              this.CURRENT_STATE.length == 0 ? "" : this.CURRENT_STATE[0],
            FACILITY_SOURCE: "",
            REMARK: "",
            OPERAT_PRESSURE: "",
            FACTORY: "",
            FACILITY_NUM: "",
            ADDR_POSITION: this.ADDR_POSITION,
            SC_DATE: "",
            ORG_NO: this.ORG_NO,
            CONS_NO: this.CONS_NO,
            P_FACILITY_ID:
              P_FACILITY_ID == "0" ? "0" : P_FACILITY_ID.FACILITY_ID,
            FACILITY_LEVEL: "",
            FACILITY_CONFIGURE: "",
            FACILITY_NO: this.FACILITY_NO,
            M_FACILITY_ID: this.M_CID == "" ? getsn() : this.M_CID,
            P_M_FACILITY_ID: P_FACILITY_ID == "0" ? "0" : P_FACILITY_ID.M_CID
          }
        ]
      };
      console.log(data);
      submitremoval(data, "RCS026", this.submitActionCallback);
    },
    submitActionCallback(result) {
      if (result.resultCode === "9999") {
        this.saveAction(2);
      }
    }
  }
};
</script>

<style scoped lang="less">
@import "../../assets/less/style.less";
</style>
<style>
@import "../../assets/less/vuxStyle.less";
</style>
